﻿namespace Ngs
{
    using System;

    public static class ExceptionFactory
    {
        static ExceptionFactory() { }

        public static bool ThrowsException<E>(Action f) where E : Exception
        {
            try
            {
                f();
            }
            catch (E)
            {
                return true;
            }
            catch
            {
            }

            return false;
        }

        public static void ThrowIfArgumentNullOrEmpty(string parameter)
        {
            if (string.IsNullOrEmpty(parameter))
            {
                throw new ArgumentNullException();
            }
        }

        public static void ThrowIfArgumentNullOrEmpty(string parameter, string name)
        {
            if (string.IsNullOrEmpty(parameter))
            {
                throw new ArgumentNullException(name);
            }
        }

        public static void ThrowIfArgumentNullOrEmpty(string parameter, string name, string message)
        {
            if (string.IsNullOrEmpty(parameter))
            {
                throw new ArgumentNullException(name, message);
            }
        }

        public static void ThrowIfArgumentNull(object parameter)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException();
            }
        }

        public static void ThrowIfArgumentNull(object parameter, string name)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(name);
            }
        }

        public static void ThrowIfNullArgumentException(object parameter, string name)
        {
            if (parameter == null)
            {
                throw new ArgumentException(name);
            }
        }

        public static void ThrowIfNullReference(object parameter, string message)
        {
            if (parameter == null)
            {
                throw new NullReferenceException(message);
            }
        }

        public static void ThrowArgumentException(bool isInvalid)
        {
            ThrowArgumentException(isInvalid, string.Empty);
        }

        public static void ThrowArgumentException(bool isInvalid, string message)
        {
            if (isInvalid)
            {
                throw new ArgumentException(message);
            }
        }

        public static void ThrowInvalidOperation(bool isInvalid)
        {
            ThrowInvalidOperation(isInvalid, string.Empty);
        }

        public static void ThrowInvalidOperation(bool isInvalid, string message)
        {
            if (isInvalid)
            {
                throw new InvalidOperationException(message);
            }
        }
    }
}

